package com.test.service.impl;

import com.test.entitys.Book;
import com.test.entitys.Borrow;
import com.test.entitys.User;
import com.test.entity.UserBorrowDetail;
import com.test.mapper.BorrowMapper;
import com.test.service.BorrowService;
import com.test.service.client.BookClient;
import com.test.service.client.UserClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BorrowServiceImpl implements BorrowService {

    @Resource
    BorrowMapper mapper;
    @Resource
    UserClient userClient;
    @Resource
    BookClient bookClient;
//    @Resource
//    RestTemplate restTemplate;
//    @Override
//    public UserBorrowDetail getUserBorrowDetailByUid(int uid) {
//        List<Borrow> borrow = mapper.getBorrowsByUid(uid);
//        //那么问题来了，现在拿到借阅关联信息了，怎么调用其他服务获取信息呢？
//        User user = restTemplate.getForObject("http://USER-SERVICE:8001/user/"+uid, User.class);
//        //获取每一本书的详细信息
//        List<Book> bookList = borrow
//                .stream()
//                .map(b -> restTemplate.getForObject("http://BOOK-SERVICE:8002/book/"+b.getBid(), Book.class))
//                .collect(Collectors.toList());
//        return new UserBorrowDetail(user, bookList);
//    }
    @Override
    public UserBorrowDetail getUserBorrowDetailByUid(int uid) {
        List<Borrow> borrow = mapper.getBorrowsByUid(uid);
        User user=userClient.findUserById(uid);
        List<Book> bookList=borrow.stream().map(b->
            bookClient.findBookById(b.getBid())
        ).collect(Collectors.toList());
        return new UserBorrowDetail(user,bookList);
    }
}
